<?php

namespace App\Http\Controllers\Backend\System;

use App\Http\Controllers\BaseController;
use App\Interfaces\RoleInterface;
use App\Interfaces\UserInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UserController extends BaseController
{
    protected UserInterface $user;

    protected RoleInterface $role;

    public function __construct(UserInterface $user, RoleInterface $role)
    {
        $this->user = $user;
        $this->role = $role;
    }

    /**
     * 检查用户是否存在
     * @param Request $request
     * @return JsonResponse
     */
    public function existence(Request $request): JsonResponse
    {
        $loginUser = $request->get('userInfo');
        $data['id'] = $request->input('id');
        $data['username'] = $request->input('username');
        $data['tenantId'] = $loginUser['tenantId'];
        $res = $this->user->existence($data);
        if (!$res){
            return $this->apiResult(['code'=>1,'message'=>'OK']);
        }
        return $this->apiResult(['code'=>0,'message'=>'Refused',$res]);
    }

    /**
     * 分页获取用户
     * @param Request $request
     * @return JsonResponse
     */
    public function getUserPage(Request $request): JsonResponse
    {
        $loginUser = $request->get('userInfo');
        $tenantId = $loginUser['tenantId'];
        $params['username'] = $request->input('username');
        $params['nickname'] = $request->input('nickname');
        $params['sex'] = $request->input('sex');
        $params['sort'] = $request->input('sort');
        $params['order'] = $request->input('order');
        $page = $request->input('page');
        $pageSize = $request->input('limit');
        $users = $this->user->getUserPage($tenantId,$params,$page,$pageSize);

        $roles = $this->role->getAllRoles();
        $role_temp = setKeyToArray($roles,'roleId');
        foreach ($users['list'] as $key=>$value)
        {
            $roleIds = json_decode($value->roles);
            unset($value->roles);
            foreach ($roleIds as $v){
                $value->roles[] = $role_temp[$v];
            }
        }
        return $this->apiResult(['code'=>0,'data'=>$users,'message'=>'success']);
    }

    /**
     * 重置密码
     * @param Request $request
     * @return JsonResponse
     */
    public function resetPassword(Request $request): JsonResponse
    {
        $userId = $request->input('userId');
        $password = $request->input('password');
        $data['userId'] = $userId;
        $data['password'] = bcrypt($password);

        $res = $this->user->updateUser($data);

        if (!$res){
            return $this->apiResult(['code'=>1,'message'=>'重置密码失败！']);
        }

        return $this->apiResult(['code'=>0,'message'=>'重置密码'.$userId.'为123456成功！']);
    }

    /**
     * 添加用户
     * @param Request $request
     * @return JsonResponse
     */
    public function createUser(Request $request): JsonResponse
    {
        $loginUser = $request->get('userInfo');
        $data = $request->input();
        $data['tenantId'] = $loginUser['tenantId'];
        $roles = array();
        foreach($data['roles'] as $value){
            $roles[] = $value['roleId'];
        }
        $data['roles'] = json_encode($roles,JSON_UNESCAPED_UNICODE);
        $data['password'] = bcrypt($data['password']);
        $data['createTime'] = dateTime();

        $res = $this->user->createUser($data);

        if (!$res){
            return $this->apiResult(['code'=>1,'message'=>'添加用户失败！']);
        }

        return $this->apiResult(['code'=>0,'message'=>'添加用户成功！']);
    }

    /**
     * 修改用户
     * @param Request $request
     * @return JsonResponse
     */
    public function updateUser(Request $request): JsonResponse
    {
        $data = $request->input();
        if (!empty($data['roles'])){
            $roles = array();
            foreach ($data['roles'] as $value){
                $roles[] = $value['roleId'];
            }
            $data['roles'] = json_encode($roles);
        }
        $data['updateTime'] = dateTime();

        unset($data['password']);

        $res = $this->user->updateUser($data);
        if (!$res){
            return $this->apiResult(['code'=>1,'message'=>'修改用户失败！']);
        }

        return $this->apiResult(['code'=>0,'message'=>'修改用户成功！']);
    }

    /**
     * 删除用户
     * @param $userId
     * @return JsonResponse
     */
    public function deleteUser($userId): JsonResponse
    {
        $data['deleted'] = 1;
        $data['updateTime'] = dateTime();
        $data['userId'] = $userId;

        $res = $this->user->updateUser($data);

       if (!$res){
            return $this->apiResult(['code'=>1,'message'=>'删除用户失败！']);
        }

        return $this->apiResult(['code'=>0,'message'=>'删除用户'.$userId.'成功！']);
    }

    /**
     * 修改用户状态
     * @param Request $request
     * @return JsonResponse
     */
    public function updateUserStatus(Request $request): JsonResponse
    {
        $userId = $request->input('userId');
        $status = $request->input('status');
        $data['userId'] = $userId;
        $data['status'] = $status;
        $data['updateTime'] = dateTime();

        $res = $this->user->updateUser($data);

        if (!$res){
            return $this->apiResult(['code'=>1,'message'=>'修改用户状态失败！']);
        }
        return $this->apiResult(['code'=>0,'message'=>'修改用户'.$userId.'状态成功！']);
    }

    /**
     * 批量删除用户
     * @param Request $request
     * @return JsonResponse
     */
    public function deleteUserBatch(Request $request): JsonResponse
    {
        $userIds = $request->input();
        $data['deleted'] = 1;
        $data['updateTime'] = dateTime();

        $res = $this->user->updateBatch($userIds,$data);

        if (!$res){
            return $this->apiResult(['code'=>1,'message'=>'批量删除用户失败！']);
        }
        return $this->apiResult(['code'=>0,'message'=>'批量删除用户成功！']);
    }
}
